package com.example.dnf.controller;

import lombok.Data;

import java.util.*;

@Data
public class A {

    public static void main(String[] args) throws InterruptedException {
        List<Person> personList = new ArrayList<>();
        Person a = new Person();
        a.setName("A");
        a.setIndex(0);

        Person b = new Person();
        b.setName("B");
        b.setIndex(0);

        Person c = new Person();
        c.setName("C");
        c.setIndex(0);
        personList.add(a);
        personList.add(b);
        personList.add(c);
        action(personList);
    }
    public static void action(List<Person> personList) throws InterruptedException {
        List<Point> points = initPoint();
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        boolean end = false;
        while(!end){
            ListIterator<Person> personListIterator = personList.listIterator();
            while(personListIterator.hasNext()){
                Person person = personListIterator.next();
                System.out.println("请" + person.getName() +"输入:");
                String s = scanner.nextLine();
                int i = random.nextInt(Integer.parseInt(s)) + 1;
                System.out.println(i);
                int personIndex = person.getIndex() + i;
                if (personIndex >= 19){
                    System.out.println(person.getName() + "胜利");
                    personListIterator.remove();
                    return;
                }
                Point point = points.get(person.getIndex() + i);
                if (point instanceof Cpoint && person.isWaitOneStep()){
                    person.setWaitOneStep(false);
                    return;
                }
                int ruleIndex = point.getRuleIndex();
                person.setIndex(ruleIndex);
                StringBuilder sb = new StringBuilder();
                sb.append(person.getName() + "选择：" + s + ", 抛出：" + i + ", 前进" + i + "步, 当前位于：" + person.getIndex());
                for (Person p : personList) {
                    if (person.getIndex() == p.getIndex()) {
                        p.setIndex(0);
                    }
                }
                if (personList.size() == 0){
                    end = true;
                }
                System.out.println(sb.toString());
            }

            Thread.sleep(100);
        }
    }


    public static List<Point> initPoint(){
        List<Point> points = new ArrayList<Point>();
        //根据如下顺序添加point AABAA CAADA AEABA  CAEAA
        points.add(new Apoint(0));
        points.add(new Apoint(1));
        points.add(new Bpoint(2));
        points.add(new Apoint(3));
        points.add(new Apoint(4));

        points.add(new Cpoint(5));
        points.add(new Apoint(6));
        points.add(new Apoint(7));
        points.add(new Dpoint(8));
        points.add(new Apoint(9));

        points.add(new Apoint(10));
        points.add(new Epoint(11));
        points.add(new Apoint(12));
        points.add(new Bpoint(13));
        points.add(new Apoint(14));

        points.add(new Cpoint(15));
        points.add(new Apoint(16));
        points.add(new Epoint(17));
        points.add(new Apoint(18));
        points.add(new Apoint(19));
        return points;
    }
}
